home *** CD-ROM | disk | FTP | other *** search
/ Programming Microsoft Visual Basic .NET / Programming Microsoft Visual Basic .NET (Microsoft Press)(X08-78517)(2002).bin / 18 gdiplus / gdidemo / graphicprimitives.vb < prev    next >
Encoding:
Text File  |  2002-02-06  |  14.2 KB  |  398 lines

  1. Imports System.Drawing.Drawing2D
  2.  
  3. Public Class DrawingPrimitives
  4.     Inherits System.Windows.Forms.Form
  5.  
  6. #Region " Windows Form Designer generated code "
  7.  
  8.     Public Sub New()
  9.         MyBase.New()
  10.  
  11.         'This call is required by the Windows Form Designer.
  12.         InitializeComponent()
  13.  
  14.         'Add any initialization after the InitializeComponent() call
  15.  
  16.     End Sub
  17.  
  18.     'Form overrides dispose to clean up the component list.
  19.     Protected Overloads Overrides Sub Dispose(ByVal disposing As Boolean)
  20.         If disposing Then
  21.             If Not (components Is Nothing) Then
  22.                 components.Dispose()
  23.             End If
  24.         End If
  25.         MyBase.Dispose(disposing)
  26.     End Sub
  27.     Friend WithEvents MainMenu1 As System.Windows.Forms.MainMenu
  28.     Friend WithEvents MenuItem1 As System.Windows.Forms.MenuItem
  29.     Friend WithEvents mnuPaint As System.Windows.Forms.MenuItem
  30.     Friend WithEvents mnuResize As System.Windows.Forms.MenuItem
  31.     Friend WithEvents mnuLines As System.Windows.Forms.MenuItem
  32.     Friend WithEvents MenuItem9 As System.Windows.Forms.MenuItem
  33.     Friend WithEvents mnuPolygons As System.Windows.Forms.MenuItem
  34.     Friend WithEvents mnuCardinal As System.Windows.Forms.MenuItem
  35.     Friend WithEvents mnuBezier As System.Windows.Forms.MenuItem
  36.     Friend WithEvents mnuPens As System.Windows.Forms.MenuItem
  37.     Friend WithEvents mnuPenAlignment As System.Windows.Forms.MenuItem
  38.     Friend WithEvents mnuDashedLines As System.Windows.Forms.MenuItem
  39.     Friend WithEvents mnuPaths As System.Windows.Forms.MenuItem
  40.  
  41.     'Required by the Windows Form Designer
  42.     Private components As System.ComponentModel.Container
  43.  
  44.     'NOTE: The following procedure is required by the Windows Form Designer
  45.     'It can be modified using the Windows Form Designer.  
  46.     'Do not modify it using the code editor.
  47.     <System.Diagnostics.DebuggerStepThrough()> Private Sub InitializeComponent()
  48.         Me.mnuPaint = New System.Windows.Forms.MenuItem()
  49.         Me.mnuResize = New System.Windows.Forms.MenuItem()
  50.         Me.mnuPenAlignment = New System.Windows.Forms.MenuItem()
  51.         Me.mnuPolygons = New System.Windows.Forms.MenuItem()
  52.         Me.MainMenu1 = New System.Windows.Forms.MainMenu()
  53.         Me.MenuItem1 = New System.Windows.Forms.MenuItem()
  54.         Me.MenuItem9 = New System.Windows.Forms.MenuItem()
  55.         Me.mnuLines = New System.Windows.Forms.MenuItem()
  56.         Me.mnuCardinal = New System.Windows.Forms.MenuItem()
  57.         Me.mnuBezier = New System.Windows.Forms.MenuItem()
  58.         Me.mnuPens = New System.Windows.Forms.MenuItem()
  59.         Me.mnuDashedLines = New System.Windows.Forms.MenuItem()
  60.         Me.mnuPaths = New System.Windows.Forms.MenuItem()
  61.         '
  62.         'mnuPaint
  63.         '
  64.         Me.mnuPaint.Index = 0
  65.         Me.mnuPaint.Text = "Draw an ellipse in Paint event"
  66.         '
  67.         'mnuResize
  68.         '
  69.         Me.mnuResize.Index = 1
  70.         Me.mnuResize.Text = "Draw an ellipse in Resize event"
  71.         '
  72.         'mnuPenAlignment
  73.         '
  74.         Me.mnuPenAlignment.Index = 8
  75.         Me.mnuPenAlignment.Text = "Pen Alignments"
  76.         '
  77.         'mnuPolygons
  78.         '
  79.         Me.mnuPolygons.Index = 4
  80.         Me.mnuPolygons.Text = "Multiple Lines and Polygons"
  81.         '
  82.         'MainMenu1
  83.         '
  84.         Me.MainMenu1.MenuItems.AddRange(New System.Windows.Forms.MenuItem() {Me.MenuItem1})
  85.         '
  86.         'MenuItem1
  87.         '
  88.         Me.MenuItem1.Index = 0
  89.         Me.MenuItem1.MenuItems.AddRange(New System.Windows.Forms.MenuItem() {Me.mnuPaint, Me.mnuResize, Me.MenuItem9, Me.mnuLines, Me.mnuPolygons, Me.mnuCardinal, Me.mnuBezier, Me.mnuPens, Me.mnuPenAlignment, Me.mnuDashedLines, Me.mnuPaths})
  90.         Me.MenuItem1.Text = "Examples"
  91.         '
  92.         'MenuItem9
  93.         '
  94.         Me.MenuItem9.Index = 2
  95.         Me.MenuItem9.Text = "-"
  96.         '
  97.         'mnuLines
  98.         '
  99.         Me.mnuLines.Index = 3
  100.         Me.mnuLines.Text = "Line, Rectangle, Ellipse"
  101.         '
  102.         'mnuCardinal
  103.         '
  104.         Me.mnuCardinal.Index = 5
  105.         Me.mnuCardinal.Text = "Cardinal splines"
  106.         '
  107.         'mnuBezier
  108.         '
  109.         Me.mnuBezier.Index = 6
  110.         Me.mnuBezier.Text = "Bezier splines"
  111.         '
  112.         'mnuPens
  113.         '
  114.         Me.mnuPens.Index = 7
  115.         Me.mnuPens.Text = "Pens"
  116.         '
  117.         'mnuDashedLines
  118.         '
  119.         Me.mnuDashedLines.Index = 9
  120.         Me.mnuDashedLines.Text = "Dashed lines and arrow caps"
  121.         '
  122.         'mnuPaths
  123.         '
  124.         Me.mnuPaths.Index = 10
  125.         Me.mnuPaths.Text = "Paths"
  126.         '
  127.         'DrawingPrimitives
  128.         '
  129.         Me.AutoScaleBaseSize = New System.Drawing.Size(7, 17)
  130.         Me.ClientSize = New System.Drawing.Size(576, 341)
  131.         Me.Font = New System.Drawing.Font("Microsoft Sans Serif", 11!, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, CType(0, Byte))
  132.         Me.Menu = Me.MainMenu1
  133.         Me.Name = "DrawingPrimitives"
  134.         Me.Text = "Drawing primitives"
  135.  
  136.     End Sub
  137.  
  138. #End Region
  139.  
  140.     Private Sub Form1_Paint(ByVal sender As Object, ByVal e As System.Windows.Forms.PaintEventArgs) Handles MyBase.Paint
  141.         If mnuPaint.Checked = False Then Exit Sub
  142.  
  143.         ' Get the Graphics object corresponding to the form's surface.
  144.         Dim gr As Graphics = e.Graphics
  145.         ' Clear the background.
  146.         gr.Clear(Color.Azure)
  147.         ' Draw an ellipse as large as the form.
  148.         gr.DrawEllipse(Pens.Red, 0, 0, Me.ClientSize.Width, Me.ClientSize.Height)
  149.     End Sub
  150.  
  151.  
  152.     Private Sub Form1_Resize(ByVal sender As Object, ByVal e As System.EventArgs) Handles MyBase.Resize
  153.         If mnuResize.Checked = False Then Exit Sub
  154.  
  155.         ' Get the Graphics object corresponding to the form's surface.
  156.         Dim gr As Graphics = Me.CreateGraphics
  157.         ' Clear the background.
  158.         gr.Clear(Color.Azure)
  159.         ' Draw an ellipse as large as the form.
  160.         gr.DrawEllipse(Pens.Red, 0, 0, Me.ClientSize.Width, Me.ClientSize.Height)
  161.         ' Destroy the Graphics object.
  162.         gr.Dispose()
  163.     End Sub
  164.  
  165.     Private Sub mnuPaint_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles mnuPaint.Click, mnuResize.Click
  166.         With CType(sender, MenuItem)
  167.             .Checked = Not .Checked
  168.         End With
  169.         ' Force a refresh.
  170.         Me.Invalidate()
  171.     End Sub
  172.  
  173.     Private Sub mnuLines_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles mnuLines.Click
  174.         ' Wait until the menu closes.
  175.         Threading.Thread.Sleep(400)
  176.  
  177.         ' Create a Graphics object.
  178.         Dim gr As Graphics = Me.CreateGraphics
  179.         ' Draw a red line from (100, 30) to (500, 300).
  180.         gr.DrawLine(Pens.Red, 100, 30, 500, 300)
  181.         ' Draw a white square with left-top corner at (130, 50) and side=300 pixels.
  182.         gr.DrawRectangle(Pens.White, New Rectangle(130, 50, 300, 300))
  183.         ' Draw a green circle with radius=200 and center at (350,250)
  184.         Dim r As Integer = 200
  185.         gr.DrawEllipse(Pens.Green, 350 - r, 250 - r, r * 2, r * 2)
  186.  
  187.         Dim r2 As Integer = 100
  188.         gr.DrawArc(Pens.Blue, 200 - r2, 150 - r2, r2 * 2, r2 * 2, 0, 90)
  189.  
  190.         ' Destroy the Graphics object.
  191.         gr.Dispose()
  192.     End Sub
  193.  
  194.     Private Sub mnuPolygons_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles mnuPolygons.Click
  195.         ' Wait until the menu closes.
  196.         Threading.Thread.Sleep(400)
  197.  
  198.         ' Create a Graphics object.
  199.         Dim gr As Graphics = Me.CreateGraphics
  200.  
  201.         ' Draw three straight lines.
  202.         Dim points() As Point = {New Point(10, 10), New Point(100, 80), New Point(200, 20), New Point(300, 100)}
  203.         gr.DrawLines(Pens.Black, points)
  204.  
  205.         ' Draw a rhomb.
  206.         gr.DrawPolygon(Pens.Red, New Point() {New Point(200, 50), New Point(300, 100), New Point(200, 150), New Point(100, 100)})
  207.  
  208.         ' Draw three rectangles.
  209.         Dim rects() As Rectangle = {New Rectangle(50, 30, 200, 100), New Rectangle(70, 40, 220, 110), New Rectangle(90, 50, 240, 120)}
  210.         gr.DrawRectangles(Pens.Green, rects)
  211.  
  212.         ' Destroy the Graphics object.
  213.         gr.Dispose()
  214.     End Sub
  215.  
  216.     Private Sub mnuCardinal_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles mnuCardinal.Click
  217.         ' Wait until the menu closes.
  218.         Threading.Thread.Sleep(400)
  219.  
  220.         ' Create a Graphics object.
  221.         Dim gr As Graphics = Me.CreateGraphics
  222.         gr.Clear(Color.White)
  223.  
  224.         Dim points() As Point = {New Point(100, 100), New Point(200, 200), New Point(250, 30), New Point(350, 100)}
  225.         Dim tension As Single
  226.  
  227.         For tension = 0 To 2 Step 0.5
  228.             gr.DrawCurve(Pens.Black, points, tension)
  229.         Next
  230.  
  231.         ' Destroy the Graphics object.
  232.         gr.Dispose()
  233.     End Sub
  234.  
  235.     Private Sub mnuBezier_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles mnuBezier.Click
  236.         ' Wait until the menu closes.
  237.         Threading.Thread.Sleep(400)
  238.  
  239.         ' Create a Graphics object.
  240.         Dim gr As Graphics = Me.CreateGraphics
  241.         gr.Clear(Color.White)
  242.  
  243.         gr.DrawBezier(Pens.Black, New Point(10, 30), New Point(100, 20), New Point(140, 190), New Point(200, 200))
  244.         gr.DrawLine(Pens.Red, New Point(10, 30), New Point(100, 20))
  245.         gr.DrawLine(Pens.Red, New Point(200, 200), New Point(140, 190))
  246.  
  247.  
  248.         ' Destroy the Graphics object.
  249.         gr.Dispose()
  250.     End Sub
  251.  
  252.     Private Sub mnuPens_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles mnuPens.Click
  253.         ' Wait until the menu closes.
  254.         Threading.Thread.Sleep(400)
  255.  
  256.         ' Create a Graphics object.
  257.         Dim gr As Graphics = Me.CreateGraphics
  258.         gr.Clear(Color.White)
  259.  
  260.         ' Draw the inner rectanble with a pen of custom color.
  261.         Dim p1 As New Pen(Color.FromArgb(128, 0, 60))
  262.         gr.DrawRectangle(p1, 50, 50, 100, 50)
  263.         ' Draw the outer rectangle with a blue pen 4 pixel wide.
  264.         Dim p2 As New Pen(Color.Blue, 4)
  265.         gr.DrawRectangle(p2, 10, 10, 200, 100)
  266.  
  267.         ' Destroy the Pen objects
  268.         p1.Dispose()
  269.         p2.Dispose()
  270.  
  271.         ' Destroy the Graphics object.
  272.         gr.Dispose()
  273.     End Sub
  274.  
  275.     Private Sub mnuPenAlignment_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles mnuPenAlignment.Click
  276.         ' Wait until the menu closes.
  277.         Threading.Thread.Sleep(400)
  278.  
  279.         ' Create a Graphics object.
  280.         Dim gr As Graphics = Me.CreateGraphics
  281.         gr.Clear(Color.White)
  282.  
  283.         Dim p As New Pen(Color.Yellow, 12)
  284.         Dim rect As New Rectangle(20, 20, 100, 100)
  285.  
  286.         gr.DrawRectangle(p, rect)
  287.         gr.DrawRectangle(Pens.Black, rect)
  288.  
  289.         p.Alignment = Drawing.Drawing2D.PenAlignment.Inset
  290.         rect.Offset(150, 0)
  291.         gr.DrawRectangle(p, rect)
  292.         gr.DrawRectangle(Pens.Black, rect)
  293.         p.Dispose()
  294.  
  295.         ' Enable this block of code to show that other alignment settings have no effect
  296. #If 0 Then
  297.         p = New Pen(Color.Red, 12)
  298.         p.Alignment = Drawing.Drawing2D.PenAlignment.Left
  299.         gr.DrawLine(p, 20, 180, 200, 180)
  300.         gr.DrawLine(Pens.Black, 20, 180, 200, 180)
  301.         p.Dispose()
  302.  
  303.  
  304.         p = New Pen(Color.Yellow, 12)
  305.         p.Alignment = Drawing.Drawing2D.PenAlignment.Right
  306.         gr.DrawLine(p, 20, 220, 200, 220)
  307.         gr.DrawLine(Pens.Black, 20, 220, 200, 220)
  308.         p.Dispose()
  309. #End If
  310.  
  311.         ' Destroy the Graphics object.
  312.         gr.Dispose()
  313.     End Sub
  314.  
  315.     Private Sub mnuDashedLines_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles mnuDashedLines.Click
  316.         ' Wait until the menu closes.
  317.         Threading.Thread.Sleep(400)
  318.  
  319.         ' Create a Graphics object.
  320.         Dim gr As Graphics = Me.CreateGraphics
  321.         gr.Clear(Color.White)
  322.  
  323.         ' a custom pen
  324.         Dim p As New Pen(Color.Black, 3)
  325.         p.DashStyle = Drawing.Drawing2D.DashStyle.Dash
  326.         gr.DrawLine(p, 10, 10, 200, 10)
  327.         p.DashStyle = Drawing.Drawing2D.DashStyle.DashDot
  328.         gr.DrawLine(p, 10, 30, 200, 30)
  329.         p.DashStyle = Drawing.Drawing2D.DashStyle.DashDotDot
  330.         gr.DrawLine(p, 10, 50, 200, 50)
  331.         p.DashStyle = Drawing.Drawing2D.DashStyle.Dot
  332.         gr.DrawLine(p, 10, 70, 200, 70)
  333.  
  334.         ' create a custom dash pattern.
  335.         Dim sngArray() As Single = {4, 4, 8, 4, 12, 4}
  336.         p.DashPattern = sngArray
  337.         gr.DrawLine(p, 10, 90, 200, 90)
  338.  
  339.         ' display pens with cap
  340.         Dim p2 As New Pen(Color.Black, 8)
  341.         p2.StartCap = Drawing.Drawing2D.LineCap.DiamondAnchor
  342.         p2.EndCap = Drawing.Drawing2D.LineCap.ArrowAnchor
  343.         gr.DrawLine(p2, 280, 30, 500, 30)
  344.  
  345.         p2.StartCap = Drawing.Drawing2D.LineCap.RoundAnchor
  346.         p2.EndCap = Drawing.Drawing2D.LineCap.Round
  347.         gr.DrawLine(p2, 280, 70, 500, 70)
  348.  
  349.  
  350.         ' Destroy the Pen object.
  351.         p.Dispose()
  352.         p2.Dispose()
  353.         ' Destroy the Graphics object.
  354.         gr.Dispose()
  355.  
  356.     End Sub
  357.  
  358.     Private Sub mnuPaths_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles mnuPaths.Click
  359.         ' Wait until the menu closes.
  360.         Threading.Thread.Sleep(400)
  361.  
  362.         ' Create a Graphics object.
  363.         Dim gr As Graphics = Me.CreateGraphics
  364.         gr.Clear(Color.White)
  365.  
  366.         ' Create a new Path object.
  367.         Dim pa As New GraphicsPath()
  368.         ' Start a figure.
  369.         pa.StartFigure()
  370.         pa.AddRectangle(New Rectangle(20, 20, 200, 150))
  371.         pa.AddRectangle(New Rectangle(50, 50, 200, 150))
  372.         pa.AddLine(20, 20, 50, 50)
  373.         pa.StartFigure()   ' Avoid connecting the two segments. 
  374.         pa.AddLine(220, 20, 250, 50)
  375.         pa.StartFigure()   ' Avoid connecting the two segments. 
  376.         pa.AddLine(220, 170, 250, 200)
  377.         pa.StartFigure()   ' Avoid connecting the two segments. 
  378.         pa.AddLine(20, 170, 50, 200)
  379.  
  380.         ' Start another (closed) figure.
  381.         pa.StartFigure()
  382.         pa.AddLine(300, 20, 400, 20)
  383.         pa.AddLine(400, 20, 400, 120)
  384.         pa.CloseFigure()
  385.  
  386.         ' Draw the path, using a thick red pen
  387.         Dim p As New Pen(Color.Red, 3)
  388.         gr.DrawPath(p, pa)
  389.  
  390.         pa.Dispose()
  391.         p.Dispose()
  392.  
  393.  
  394.         ' Destroy the Graphics object.
  395.         gr.Dispose()
  396.     End Sub
  397. End Class
  398.